package com.tuiyun.jxgl.dao;

// Generated Feb 1, 2013 4:10:46 PM by Hibernate Tools 3.4.0.CR1

import java.util.Collections;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.tuiyun.jxgl.bean.Driver;
import com.tuiyun.jxgl.bean.Student;
import com.tuiyun.jxgl.bean.Stuselectdriver;

import static org.hibernate.criterion.Example.create;

/**
 * Home object for domain model class Stuselectdriver.
 * @see com.tuiyun.jxgl.Stuselectdriver
 * @author Hibernate Tools
 */
public class StuselectdriverHome extends HibernateDaoSupport{

	private static final Log log = LogFactory.getLog(StuselectdriverHome.class);	

	public void persist(Stuselectdriver transientInstance) {
		log.debug("persisting Stuselectdriver instance");
		try {
			getHibernateTemplate().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}
	public Driver findDrierByStudent(int stuid){
		log.debug("getting Driver instance with stuid: " + stuid);
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "select d from Driver d,Stuselectdriver sd where sd.stuid = ? and sd.driver.id = d.id";
			Query q = s.createQuery(sql);
			q.setInteger(0, stuid);
			return (Driver)q.uniqueResult();
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public Stuselectdriver findByStudent(int id){
		log.debug("getting Stuselectdriver instance with stuid: " + id);
		try {
			Session s = this.getHibernateTemplate().getSessionFactory().openSession();
			Stuselectdriver instance = (Stuselectdriver) s.createQuery("from com.tuiyun.jxgl.bean.Stuselectdriver sd where sd.stuid = "+id)
					.uniqueResult();
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public void attachDirty(Stuselectdriver instance) {
		log.debug("attaching dirty Stuselectdriver instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Stuselectdriver instance) {
		log.debug("attaching clean Stuselectdriver instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Stuselectdriver persistentInstance) {
		log.debug("deleting Stuselectdriver instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Stuselectdriver merge(Stuselectdriver detachedInstance) {
		log.debug("merging Stuselectdriver instance");
		try {
			Stuselectdriver result = (Stuselectdriver) getHibernateTemplate().
					merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Stuselectdriver findById(int id) {
		log.debug("getting Stuselectdriver instance with id: " + id);
		try {
			Stuselectdriver instance = (Stuselectdriver) getHibernateTemplate().
					get("com.tuiyun.jxgl.bean.Stuselectdriver",
							id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Stuselectdriver> findByExample(Stuselectdriver instance) {
		log.debug("finding Stuselectdriver instance by example");
		try {
			List<Stuselectdriver> results = (List<Stuselectdriver>) getHibernateTemplate().
					findByExample(instance);					
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public int getDriverCommentCount(int driverid) {		
		log.debug("getting Comment instances size");
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			String sql = "select count(*) from com.tuiyun.jxgl.bean.Stuselectdriver s " +
					"where s.driver.id = ? and s.comment_time != null";			
			Query q = s.createQuery(sql);			
			q.setInteger(0, driverid);
			Integer res = new Integer(String.valueOf((Long) q.uniqueResult()));
			s.close();
			return res;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Stuselectdriver> findbyDriver(int driverid) {
		log.debug("getting Comment instances with driver " + driverid);
		try {
			Session s = this.getHibernateTemplate().getSessionFactory()
					.openSession();
			Query q = s.createQuery("from com.tuiyun.jxgl.bean.Stuselectdriver s where s.driver.id = ? and s.comment_time != null");	
			q.setInteger(0, driverid);
			List<Stuselectdriver> ds = q.list();
			s.close();
			return ds;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			return Collections.EMPTY_LIST;
		}
	}	
}
